package com.sjt.thread;

public class ThreadTest1 {
    public static void main(String[] args) throws InterruptedException {
        Interrupt interrupt=new Interrupt();
        interrupt.start();
        Thread.sleep(3500);
        System.out.println("to stop");
        interrupt.stop();
    }
}
class Interrupt{
    private Thread thread;

    public void start(){
        thread = new Thread(() -> {
            while(true) {
                if(thread.isInterrupted()) {
                    System.out.println(thread.getName()+":中断线程");
                    break;
                }
                try {
                    Thread.sleep(1000);
                    System.out.println(thread.getName()+":继续运行");
                } catch (InterruptedException e) {
                    thread.interrupt();
                }

            }
        });
        thread.setName("监控线程");
        thread.start();
    }

    public void stop()
    {
        thread.interrupt();
    }
}